package com.yaozheng.vocationaltraining.adapter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.CompetingProductsPKItemView;
import com.yaozheng.vocationaltraining.view.CompetingProductsPKItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 竞品PK设备器
 * 
 * @author Administrator
 * 
 */
public class CompetingProductsPKAdapter extends BaseAdapter {
	private BaseActivity baseActivity;

	JSONArray dataList;

	// DisplayImageOptions displayImageOptions;

	BitmapLoader bitmapLoader;

	public CompetingProductsPKAdapter(BaseActivity baseActivity,
			JSONArray dataList) {
		super();
		this.baseActivity = baseActivity;
		this.dataList = dataList;
		bitmapLoader = new BitmapLoader(Glide.with(baseActivity),
				R.drawable.image_2);
	}

	public JSONArray getDataList() {
		return dataList;
	}

	/**
	 * 得到最后id
	 * 
	 * @return
	 */
	public int getLastId() {
		if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {
			JSONObject itemData = TypeUtils.getJsonObject(dataList,
					TypeUtils.getJsonArraySize(dataList) - 1);
			if (itemData != null) {
				return TypeUtils.getJsonInteger(itemData, "id", -1);
			}
		}
		return -1;
	}

	@Override
	public int getCount() {
		return TypeUtils.getJsonArraySize(dataList);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CompetingProductsPKItemView competingProductsPKItemView;
		if (convertView == null) {
			competingProductsPKItemView = CompetingProductsPKItemView_
					.build(baseActivity);
		} else {
			competingProductsPKItemView = (CompetingProductsPKItemView) convertView;
		}
		competingProductsPKItemView.loadData(
				TypeUtils.getJsonObject(dataList, position), position,
				bitmapLoader);
		return competingProductsPKItemView;
	}
}
